package com.scfinance.SPD.service;

import com.alibaba.fastjson.JSONObject;
import com.scfinance.SPD.dao.OrderDaoImpl;
import com.scfinance.SPD.util.PropsUtil;
import com.scfinance.SPD.util.Validator;
import com.scfinance.SPD.util.http.HttpRequester;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by admin on 2018/1/9.
 */
@Service
public class SPDSimpleService {

    private Logger logger = LoggerFactory.getLogger(SPDSimpleService.class);

    @Autowired
    private OrderDaoImpl orderDao;

    private static final String SPDOUTPUT_REJECT  = "2";
    private static final String SPDOUTPUT_SUCCESS = "0";

    public void pushSPDEntry(String loanOrderId) {
        try {
            Object[] params = {loanOrderId};
            Map sqlMap = orderDao.pushSPDEntry(params);
            Map<String, String> pushMap = new HashMap<>();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            pushMap.put("customerName", (String) sqlMap.get("customerName"));
            pushMap.put("time", format.format(sqlMap.get("time")));
            pushMap.put("AppId", (String) sqlMap.get("AppId"));
            pushMap.put("userId", (String) sqlMap.get("userId"));
            pushMap.put("orderId", (String) sqlMap.get("orderId"));
            logger.info("浦发推送：", pushMap);
            String s = HttpRequester.sendPost(PropsUtil.getProperty("SPDPushUrl"), pushMap);
            logger.info("浦发推送返回：", s);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("浦发推送失败" + e.getMessage());
        }
    }

    public String updateSPDreturn(Object[] stayPatchParams) {
        String orderId = null;
        Object[] params = {stayPatchParams[3]};
        Map SPDReturn = orderDao.getSingleSPDReturn(params);
        if (SPDReturn != null){
            orderId = (String) SPDReturn.get("orderId");
            Object[] miniStatusParams = {orderId};
            Map resultMap = orderDao.findMiniStatus(miniStatusParams);
            if (Validator.isNotNull(resultMap)
                    && Validator.isNotNull(resultMap.get("value"))
                    && "2".equals(String.valueOf(resultMap.get("value")))) {
                Boolean returnFlag = orderDao.updateSPDreturn(stayPatchParams);
                logger.info("更新浦发返回信息__" + (returnFlag ? "成功" : "失败"));
                System.out.println("更新浦发返回信息__" + (returnFlag ? "成功" : "失败"));
            }else {
                logger.info("已经存在待补件记录，无需更新");
                System.out.println("已经存在待补件记录，无需更新");
            }
        }else {
            logger.info("无需更新");
        }
        return orderId;
    }

    public void saveSPDReturn(String appId, Map<String, Object> map, String loanOrderId, JSONObject returnJson) {
        Boolean returnFlag = null;
        Map numMap = orderDao.findSPDReturn(loanOrderId);
        Object[] paramsObj = new Object[5];
        if ((Long) numMap.get("num") == 0) {
            paramsObj[0] = loanOrderId;
            paramsObj[1] = map.get("shenqingrenzhongwenminchen");
            paramsObj[2] = returnJson.getString("errorCode");
            paramsObj[3] = returnJson.getString("errorMsg");
            paramsObj[4] = appId;
            logger.info("新建浦发返回信息__" + paramsObj[0] + paramsObj[1] + paramsObj[3]);
            System.out.println("新建浦发返回信息__" + paramsObj);
            returnFlag = orderDao.saveSPDReturn(paramsObj);
            logger.info("新建浦发返回信息__" + (returnFlag ? "成功" : "失败"));
            System.out.println("新建浦发返回信息__" + (returnFlag ? "成功" : "失败"));
        } else {
            paramsObj[0] = map.get("shenqingrenzhongwenminchen");
            paramsObj[1] = returnJson.getString("errorCode");
            paramsObj[2] = returnJson.getString("errorMsg");
            paramsObj[3] = appId;
            paramsObj[4] = loanOrderId;
            logger.info("更新浦发返回信息__" + paramsObj[4] + paramsObj[1] + paramsObj[3]);
            System.out.println("更新浦发返回信息__" + paramsObj);
            returnFlag = orderDao.updateSPDReturn(paramsObj);
            logger.info("更新浦发返回信息__" + (returnFlag ? "成功" : "失败"));
            System.out.println("更新浦发返回信息__" + (returnFlag ? "成功" : "失败"));
        }

    }

    public void saveOperationRecord(Map<String, Object> map, String transaction_id, String transaction_name) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        Object[] recordParams = {uuid, map.get("pi_id"), String.valueOf(map.get("status")), map.get("status_name"), transaction_id, transaction_name, "System", "System"};
        logger.info("保存operationRecord__" + recordParams);
        System.out.println("保存operationRecord__" + recordParams);
        Boolean returnFlag = orderDao.saveOperationRecord(recordParams);
        logger.info("保存operationRecord__" + (returnFlag ? "成功" : "失败"));
        System.out.println("保存operationRecord__" + (returnFlag ? "成功" : "失败"));
    }

    public Boolean updateSingleFlag(Object[] params) {
        Boolean returnFlag = orderDao.updateSingleFlag(params);
        logger.info("更新发送的订单状态__" + (returnFlag ? "成功" : "失败"));
        System.out.println("更新发送的订单状态__" + (returnFlag ? "成功" : "失败"));
        return returnFlag;
    }

    public Boolean setSPDOutputDefault(String orderId) {
        Object[] params = {SPDOUTPUT_REJECT, orderId};
        Boolean returnFlag = orderDao.setSPDOutputFlag(params);
        logger.info("更新发送的状态__" + (returnFlag ? "成功" : "失败"));
        System.out.println("更新发送的状态__" + (returnFlag ? "成功" : "失败"));
        return returnFlag;
    }

    public Boolean setSPDOutputSuccess(String orderId) {
        Object[] params = {SPDOUTPUT_SUCCESS, orderId};
        Boolean returnFlag = orderDao.setSPDOutputFlag(params);
        System.out.println("更新发送的状态__" + (returnFlag ? "成功" : "失败"));
        logger.info("更新发送的状态__" + (returnFlag ? "成功" : "失败"));
        return returnFlag;
    }
}
